package com.shakespace.artofandroid.temp

import java.util.*

/**
    1. 几乎所有的表达式都有值， 除了循环
    2. 高阶函数，最好将函数作为最后一个参数
    3. 所有var 变量名都有下划线。。。。
    4. 可以添加多个init （按添加顺序执行）
        Parameters of the primary constructor can be used in the initializer blocks.
        Any properties used in initializer blocks must be declared previously.
    5. Kotlin 建议只使用一个构造函数，因为使用多个构造会导致更多的代码路径，很可能会测试不到，在写次构造函数之前，考虑下factory function能否替代。
        工厂模式：有名字/可以返回子类/任意时间调用/
        Factory functions can be inline and so their type parameters can be reified
        不足： 不能被子类直接使用
    6.Every secondary constructor must call the primary constructor first, either directly using this(),
        or indirectly by calling another secondary constructor. This means that any init blocks in the primary will be called for all constructors,
        and all the code in the primary constructor will be executed first.
    7. Subclasses must declare their constructor parameters explicitly.
        class TowerTank (override var height: Int, var diameter: Int): Aquarium(height = height, width = diameter, length = diameter) {
    8. Files don't have to have the same name as their class, and you can have multiple classes in a file.
 */

var a = 123
fun main(args: Array<String>) {

//    feedTheFish()
    val aquarium = Aquarium()
//    check(false)

//    check(false){
//        println("error occur")
//    }


}

fun randomDay() : String {
    val week = arrayOf ("Monday", "Tuesday", "Wednesday", "Thursday",
        "Friday", "Saturday", "Sunday")
    return week[Random().nextInt(week.size)]
}

fun fishFood (day : String) : String {
    return when (day) {
        "Monday" -> "flakes"
        "Wednesday" -> "redworms"
        "Thursday" -> "granules"
        "Friday" -> "mosquitoes"
        "Sunday" -> "plankton"
        else -> "nothing"
    }
}

fun feedTheFish() {
    val day = randomDay()
    val food = fishFood(day)
    println ("Today is $day and the fish eat $food")
}

fun isTooHot(temperature: Int) = temperature > 30

fun isDirty(dirty: Int) = dirty > 30

fun isSunday(day: String) = day == "Sunday"

/**
 * 默认参数不一定是一个值，也可以来自一个表达式
 * fun shouldChangeWater (day: String, temperature: Int = 22, dirty: Int = getDirtySensorReading()): Boolean {　...
 * 不要放一些耗时操作的方法
 */
fun shouldChangeWater (day: String, temperature: Int = 22, dirty: Int = 20): Boolean {
    return when {
        isTooHot(temperature) -> true
        isDirty(dirty) -> true
        isSunday(day) -> true
        else  -> false
    }
}

open class Aquarium (){
    open var a = 2
    init {
        println("init1")
    }
    init {
        println("init2")
    }

    constructor(num:Int):this(){
        println("second")
    }
}

class TowerTank() : Aquarium(){
    override var a = 3
}

